import random

class Room:
    # 固化实例化，用于存储所有房间的实例
    _instances = {}

    def __new__(cls, room_id):
        if room_id in cls._instances:
            return cls._instances[room_id]
        instance = super().__new__(cls)
        cls._instances[room_id] = instance
        return instance

    def __init__(self, room_id):
        self.room_id = room_id
        self.players = []

    def join(self, player):
        if len(self.players) < 2:
            self.players.append(player)
            player.room = self
            print(f"Player {player.id} joined room {self.room_id}")
        else:
            print("The room is full")

    def play(self):
        if len(self.players) != 2:
            print("The room is not ready")
            return
        player1, player2 = self.players
        while True:
            print("Press any key to start or 'q' to quit")
            choice = input().strip()
            if choice == 'q':
                break
            p1_choice = player1.guess()
            p2_choice = player2.guess()
            result = judge(p1_choice, p2_choice)
            print(f"Player {player1.id} chooses {p1_choice}")
            print(f"Player {player2.id} chooses {p2_choice}")
            if result == 1:
                print(f"Player {player1.id} wins")
                player2.lose()
            elif result == -1:
                print(f"Player {player2.id} wins")
                player1.lose()
            else:
                print("Tie")

            if player1.beans <= 0:
                print(f"Player {player1.id} is out of beans")
                break
            elif player2.beans <= 0:
                print(f"Player {player2.id} is out of beans")
                break

class Player:
    _id = 0

    def __init__(self):
        self.id = Player._id
        Player._id += 1
        self.beans = 100
        self.room = None

    def guess(self):
        choices = ['rock', 'paper', 'scissors']
        return random.choice(choices)

    def lose(self):
        self.beans -= 100
        print(f"Player {self.id} loses, {self.beans} beans left")

def judge(p1, p2):
    if p1 == p2:
        return 0
    elif p1 == 'rock':
        return 1 if p2 == 'scissors' else -1
    elif p1 == 'paper':
        return 1 if p2 == 'rock' else -1
    elif p1 == 'scissors':
        return 1 if p2 == 'paper' else -1

# 创建房间
room1 = Room(1)

# 创建两个玩家
player1 = Player()
player2 = Player()

# 玩家加入房间
room1.join(player1)
room1.join(player2)

# 玩家开始游戏
room1.play()
